/*
** init_serv.c for zappy in /home/morgan_t/zappy-2015-2014s-suon_a/server
** 
** Made by thomas morgant
** Login   <morgan_t@epitech.net>
** 
** Started on  Mon Jun 18 17:36:00 2012 thomas morgant
** Last update Thu Jul 12 16:46:16 2012 thomas morgant
*/

#include "server.h"
#include "common.h"

int	look_for_fd(int	fd_max)
{
  while (fd_max < MAX_FD)
    {
      fd_max++;
    }
  return (fd_maxx);
}

void	ig_serv(t_serv *serv, t_env *env)
{
  struct	timeval	tv;
  
  FD_ZERO(&env->readfd);
  FD_ZERO(&env->writefd);
  FD_SET(serv->sock, &env->readfd);
  env->fd_max = look_for_fd(env->fd_max);
  xselect(env->fd_max + 1, &env->readfd, &env->writefd, NULL, &tv);
  if (FD_ISSET(serv->sock, &env->readfd))
    add_player(serv, env);
  /* ig_client(serv, env); */
}

int	run_serv(t_serv *serv)
{
  int	s;
  struct	sockaddr_in	sin;

  s = socket(PF_INET, SOCK_STREAM, 0);
  sin.sin_family = AF_INET;
  sin.sin_port = htons(serv->port);
  sin.sin_addr.s_addr = INADDR_ANY;
  xbind(s, sin, sizeof(sin));
  return (s);
}

void	init_env(t_env *env);
{
}

void	init_serv(t_serv *serv)
{
  t_env	env;

  serv->sock = run_serv(serv);
  xlisten(serv->sock, 42);
  init_env(&env);
  while (42)
    ig_serv(serv, &env);
}
